package component;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentListener;

public class DataPanel extends JPanel{

	/**
	 * The label of the text field to edit the name of the NodeElement.
	 */
	private JLabel nameLabel;

	private JTextField nameField;

	// Class of the instance
	private JLabel classLabel;

	private JTextField classField;

	public DataPanel(DocumentListener nomDocumentListener, DocumentListener classDocumentListener){
		super(new GridBagLayout());
		
		// Building the selection grid
		buildNameLabel();
		buildNameField(nomDocumentListener);
		buildClassifierLabel();
		buildClassifierField(classDocumentListener);	
	}
	
	
	public DataPanel() {
		this(null, null);
	}
	
	private void buildClassifierField(DocumentListener classDocumentListener) {
		// Instantiate the component
		classField = new JTextField(8);
		// Disable it by default to avoid editing without a selection
		classField.setEnabled(false);
		// Place a listener to transfer text updates to the model element
		if(classDocumentListener!=null)
		classField.getDocument().addDocumentListener(
				classDocumentListener);
		// Link the label to the field
		classLabel.setLabelFor(classField);

		// Constraints for the layout
		final GridBagConstraints constraints = new GridBagConstraints();
		// First cell of the second row
		constraints.gridx = 1;
		constraints.gridy = 1;
		// Grab a full size of the extra space
		constraints.weightx = 1;
		// Grab a full size of the extra space
		constraints.weightx = 1;
		// Expand the component to fill all its display area
		constraints.fill = GridBagConstraints.HORIZONTAL;

		add(classField, constraints);
	}

	private void buildClassifierLabel() {
		// Instantiate the component
		classLabel = new JLabel("Instance Classifier");
		// Specify how the text renders inside the component
		nameLabel.setHorizontalTextPosition(SwingConstants.LEADING);

		// Constraints for the layout
		final GridBagConstraints constraints = new GridBagConstraints();
		// First cell of the second row
		constraints.gridx = 0;
		constraints.gridy = 1;
		// Grab 10% of the extra space
		constraints.weightx = 0.1;
		// Expand the component to fill all its display area
		constraints.fill = GridBagConstraints.HORIZONTAL;
		// Place a 5px space gap on the on left and right
		constraints.insets.left = 5;
		constraints.insets.right = 5;

		// Now add it to this JPanel instance
		add(classLabel, constraints);
	}

	private void buildNameField(DocumentListener nomDocumentListener) {
		// Instantiate the component
		nameField = new JTextField(8);
		// Disable it by default to avoid editing without a selection
		nameField.setEnabled(false);
		// Place a listener to transfer text updates to the model element
		if(nomDocumentListener!=null)
		nameField.getDocument().addDocumentListener(nomDocumentListener);
		// Link the label to the field
		nameLabel.setLabelFor(nameField);

		// Constraints for the layout
		final GridBagConstraints constraints = new GridBagConstraints();
		// Second cell of the first row
		constraints.gridx = 1;
		constraints.gridy = 0;
		// Grab a full size of the extra space
		constraints.weightx = 1;
		// Expand the component to fill all its display area
		constraints.fill = GridBagConstraints.HORIZONTAL;

		// Now add it to this JPanel instance
		add(nameField, constraints);
	}

	private void buildNameLabel() {
		// Instantiate the component
		nameLabel = new JLabel("Instance Name");
		// Specify how the text renders inside the component
		nameLabel.setHorizontalTextPosition(SwingConstants.LEADING);

		// Constraints for the layout
		final GridBagConstraints constraints = new GridBagConstraints();
		// First cell of the first row
		constraints.gridx = 0;
		constraints.gridy = 0;
		// Grab 10% of the extra space
		constraints.weightx = 0.1;
		// Expand the component to fill all its display area
		constraints.fill = GridBagConstraints.HORIZONTAL;
		// Place a 5px space gap on the on left and right
		constraints.insets.left = 5;
		constraints.insets.right = 5;

		// Now add it to this JPanel instance
		add(nameLabel, constraints);
	}

	public String getNameInstance(){

		return nameField.getText();
		
	}
	
	public String getClassInstance(){
		
		return classField.getText();
		
	}
	
	public void setNameInstance(String name){

		 nameField.setText(name);
		 nameField.setEnabled(true);
		
	}
	
	public void setClassInstance(String name){
		
		 classField.setText(name);
		 classField.setEnabled(true);		
	}
	
}
